package org.techmore.biz;

import java.util.ArrayList;

import org.techmore.R;
import org.techmore.activity.CouponStoreDetailsActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class CouponMapOverlayView extends ItemizedOverlay<OverlayItem> {
	
	private Context context = null;
	private MapView mapView = null;
	private View mapPopView = null;
	private ArrayList<OverlayItem> overlays = new ArrayList<OverlayItem>();

	public CouponMapOverlayView(Context context, MapView mapView,View mapPopView, Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
		this.context = context;
		this.mapView = mapView;
		this.mapPopView = mapPopView;
	}

	@Override
	protected OverlayItem createItem(int i) {
		// TODO Auto-generated method stub
		return overlays.get(i);
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		return overlays.size();
	}
	
	public void addOverlay(OverlayItem overlay) {
		overlays.add(overlay);
		//填充本次
		populate();
	}
	
	String[] title = null;
	@Override
	protected boolean onTap(int index) {
		// TODO Auto-generated method stub
		OverlayItem overlayItem = overlays.get(index);
		
		if(overlayItem.getTitle().indexOf("##")>0){
			title = overlayItem.getTitle().split("##");
		}else{
			title = new String[]{overlayItem.getTitle()};
		}
		
		String[] snippet = overlayItem.getSnippet().split("##");
		
		Button imageGOButton = (Button) mapPopView.findViewById(R.id.map_pop_button);
		if(null != title[1] && !"".equals(title[1])){
			imageGOButton.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					///////
					//title[1]
					Intent intent = new Intent(context,CouponStoreDetailsActivity.class);
					intent.putExtra("serNo", title[1]);
					context.startActivity(intent);
				}
			});
		}
		if(context.getResources().getString(R.string.mapUserlocation).equals(title[0])){
			imageGOButton.setVisibility(View.GONE);
		}else{
			imageGOButton.setVisibility(View.VISIBLE);
		}
		
		if(mapPopView.isShown()){
			mapView.removeAllViews();
		}else{
			mapView.removeAllViews();
			
			GeoPoint point = new GeoPoint(Integer.valueOf(snippet[0]), Integer.valueOf(snippet[1]));
			
			TextView textView = (TextView) mapPopView.findViewById(R.id.map_pop_textView);
			textView.setText(title[0]);
			
			MapView.LayoutParams params = new MapView.LayoutParams(MapView.LayoutParams.WRAP_CONTENT,
	    			MapView.LayoutParams.WRAP_CONTENT, point,
	    			MapView.LayoutParams.BOTTOM_CENTER);
			
			mapView.addView(mapPopView, params);
		}
		
//		System.out.println(snippet[0]+"||||"+snippet[1]);
		
		return true;
	}

}
